package agngui.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * A frame for viewing images
 * 
 * @author Ben Bloom
 */
public class ImageFrame {

	/**
	 * the window that is displayed and contains all the components
	 */
	private JFrame window;
	
	/**
	 * the file chooser used for saving images
	 */
	private JFileChooser file_chooser;
	
	/**
	 * creates a new image frame using the given image
	 * 
	 * @param image The image to display
	 */
	public ImageFrame(final BufferedImage image) {
		window = new JFrame("Image");
		window.setSize(image.getWidth(), image.getHeight());
		window.setLocationByPlatform(true);
		final JLabel label = new JLabel();
		
		label.setIcon(new ImageIcon(image));
		file_chooser = new JFileChooser(".");
	
		JMenuBar menu_bar = new JMenuBar();
		window.setJMenuBar(menu_bar);
		
		JMenu file_menu = new JMenu("File");
		JMenuItem save_item = new JMenuItem("Save");
		JMenuItem close_item = new JMenuItem("Close");
		
		file_menu.setMnemonic(KeyEvent.VK_F);
		close_item.setMnemonic(KeyEvent.VK_C);
		save_item.setMnemonic(KeyEvent.VK_S);
		
		menu_bar.add(file_menu);
		file_menu.add(save_item);
		file_menu.addSeparator();
		file_menu.add(close_item);
		
		save_item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int choice = file_chooser.showSaveDialog(window);
				if(choice == JFileChooser.APPROVE_OPTION) {
					try {
						File file = file_chooser.getSelectedFile();
						String format = file.getName();
						if(format.lastIndexOf(".") <= format.lastIndexOf(ScatteringGUI.file_separator)) {
							throw new IOException("Must specify a format for the image file (ex: png, jpg, bmp...)");
						}

						format = format.substring(format.lastIndexOf(".")+1);

						String lower_format = format.toLowerCase();
						String[] names = javax.imageio.ImageIO.getWriterFormatNames();
						boolean good = false;
						for(String name : names) {
							if(name.toLowerCase().equals(lower_format)) {
								good = true;
							}
						}

						if(!good) {
							StringBuilder message = new StringBuilder("Invalid image format selected, \"" + format + "\".  Image format must" + ScatteringGUI.endl
									+ "be one of: ");
							for(int i = 0; i < names.length; i++) {
								String name = names[i];
								//don't worry about including uppercase names in the output.
								if(name.charAt(0) >= 'A' && name.charAt(0) <= 'Z') {
									continue;
								}
								message.append(names[i]);
								if(i < names.length-1) {
									message.append(", ");
								}
							}
							throw new IOException(message.toString());
						}
						javax.imageio.ImageIO.write(image, format, file);
					} catch(IOException ioe) {
						error("Failed to write image file: " + ioe.getMessage());
					}
				}
			}
		});
		
		close_item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				window.dispose();
			}
		});
		
		window.getContentPane().add(label);
		window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		window.setVisible(true);
	}
	
	/**
	 * displays an error message
	 * 
	 * @param message The message to display
	 */
	private void error(String message) {
		JOptionPane.showMessageDialog(window, message, "Error!", JOptionPane.ERROR_MESSAGE);
	}
}
